def findpet(range):
    petfilter = Mobiles.Filter()
    petfilter.Enabled = True
    petfilter.RangeMin = 0
    petfilter.RangeMax = range + 3
    #petfilter.Name = 'a bull'
    petfilter.Name = 'a cow'
    #petfilter.Name = 'a grizzly bear'
    #petfilter.Name = 'a great hart'

    sfpet = Mobiles.ApplyFilter(petfilter)
    if len(sfpet)>0:
        return sfpet
    else:
        return False

def onestep(x1,y1):  
    x = Player.Position.X
    y = Player.Position.Y
    while x != x1 or y != y1:

        if abs(x-x1)>1 or abs(y-y1)>1:
            return(False)

        if x1 > x:
            if y1 < y:
                Player.Run('Right',False)
            elif y1 == y:
                Player.Run('East',False)
            elif y1 > y:
                Player.Run('Down',False)
        elif x1 == x:
            if y1 < y:
                Player.Run('North',False)
            elif y1 > y:
                Player.Run('South',False)
        elif x1 < x:
            if y1 < y:
                Player.Run('Up',False)
            elif y1 == y:
                Player.Run('West',False)
            elif y1 > y:
                Player.Run('Left',False)
        Misc.Pause(100) 
        x = Player.Position.X
        y = Player.Position.Y         
    return(True)

def autorun(mx,my):
    list = PathFinding.GetPath(mx, my,True)
    if list:
        for l in list:

            onestep(l.X,l.Y)
            dw = onestep(l.X,l.Y)
            if not dw:
                break
        if not dw:
            autorun(mx,my)        
        
petsz = []       
        
if Player.Followers>=1:
    Player.HeadMessage(38,"请寄存你的宠物！")
    tingzhi = True

#zongshu =0 
while True:   
    xunshouwz = PathFinding.Route()
    #xunshouwz.X = Player.Position.X
    #xunshouwz.Y = Player.Position.Y
    xunshouwz.X = 1320
    xunshouwz.Y = 1815
    
    PathFinding.Go(xunshouwz)
    Misc.Pause(3000)

    dongwu = findpet(13)
    
    #for cw in dongwu:
        #if cw.Serial not in petsz:
            #bullmo = cw
            #break
    if dongwu:  
        #bullmo = Mobiles.FindBySerial(dongwu) 
        bullmo = Mobiles.Select(dongwu,'Nearest') 
        while bullmo != None and bullmo not in petsz:
            if Player.InRangeMobile(bullmo, 2) is False:
                mobilePosition = bullmo.Position
                mobileCoords = PathFinding.Route()
                mobileCoords.X = mobilePosition.X
                mobileCoords.Y = mobilePosition.Y
                autorun(mobileCoords.X,mobileCoords.Y)
                #PathFinding.Go(mobileCoords)
                Misc.Pause(100)
                
            if Player.InRangeMobile(bullmo, 2):
                Player.UseSkill('Animal Taming')
                Target.WaitForTarget(1000,0)
                Target.TargetExecute(bullmo)
                Misc.Pause(2000)
                if bullmo.Hits == 0:
                    break
                    
            if bullmo == None:
                break
            if Player.Followers == 1:
                Player.ChatSay(18,'All Follow Me')
                Misc.Pause(2000)        
                petf = False
                while True:
                    petf = findpet(0)
                    if petf:
                        pets = Mobiles.Select(petf,'Nearest')
                        petsz.append(pets.Serial)
                        Misc.PetRename(pets.Serial,"www")
                        #petsz.append(petf)
                        #Misc.PetRename(petf,"kkk")
                        Misc.Pause(1500)
                        break
                        
                Misc.WaitForContext(pets, 1000)
                Misc.ContextReply(pets, 9)
                Misc.Pause(1000)
                Gumps.WaitForGump(Gumps.CurrentGump(), 1000)
                Gumps.SendAction(Gumps.CurrentGump(), 2)
                Misc.Pause(1000)
                break